package com.example.comzg24_2.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.url.mvvm.BaseActivity
import com.example.comzg24_2.MainActivity
import com.example.comzg24_2.R
import com.example.comzg24_2.databinding.ActivityLoginBinding
import com.example.comzg24_2.updata.UpDataActivity
@Route(path = "/app/loginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {
    override fun initView() {

        val account = SPUtils.getInstance().getString("account")
        val password = SPUtils.getInstance().getString("password")
        binding.etName.setText(account)
        binding.etPass.setText(password)


        binding.btnLogin.setOnClickListener {
            if(!RegexUtils.isMobileExact(binding.etName.text.toString())){
                ToastUtils.showLong("格式有误")
                return@setOnClickListener
            }
            viewModel.Login(binding.etName.text.toString(),binding.etPass.text.toString())
        }

        binding.cb.setOnClickListener {
            if(binding.cb.isChecked){
                SPUtils.getInstance().put("account",binding.etName.text.toString())
                SPUtils.getInstance().put("password",binding.etPass.text.toString())
            }
        }

        binding.tvPwd.setOnClickListener {
            startActivity(Intent(this,UpDataActivity::class.java))
        }
    }

    override fun initData() {
        viewModel.loginSuccess.observe(this){
            if(it.code==200){
                SPUtils.getInstance().put("token",it.data.token)
                ToastUtils.showLong(it.message)
                startActivity(Intent(this,MainActivity::class.java))
            }
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_login
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }
}